package dominio;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * Created by Rafael Simon Garcia on 12/8/13.
 */
public abstract class OptimizationAlgorithm {
    //static attributes
    //original nodes list
    protected static ArrayList<? extends GraphElement> nodesList;
    //max amount of iterations by default
    protected static int maxIterations = 100000;
    protected static boolean operatorType;


    protected static Method HasNextValue, NextValue, Operate, Calculate;

    @SuppressWarnings("finally")
	public static ArrayList<GraphElement> RearrangeNodesFromTour(ArrayList<Integer> tour){
        ArrayList<GraphElement> newNodes = new ArrayList<>(tour.size());
        try{
            //iterate over the tour ArrayList collection and add into newNodes the original item indicated
            //by the i value in tour
            for(int i: tour) newNodes.add(nodesList.get(i));
        }
        catch (IndexOutOfBoundsException e){}
        finally {
            return newNodes;
        }
    }

    public static ArrayList<Integer> DefaultTour(int length){
        ArrayList<Integer> result = new ArrayList<>(length);

        for(int i=0; i< length; i++) result.add(i);

        return result;
    }

    protected static void SetupAlgorithmReflection(Class op, Class c) throws Exception{
        //get the NextValue method from the operator
        HasNextValue = op.getDeclaredMethod("HasNextValue", null);
        //get the Operate method from the operator
        Operate = op.getDeclaredMethod("Operate", ArrayList.class);

        //get the Calculate method from the Class used to calculate how good the path is
        Calculate = c.getDeclaredMethod("Calculate", new Class[] { ArrayList.class, ArrayList.class });
    }

    public static PathOptimizationResult OptimizePath(){ return null;}

}
